/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.zhaojiu.service.impl;

import me.zhengjie.modules.zhaojiu.domain.Attendance;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.zhaojiu.repository.AttendanceRepository;
import me.zhengjie.modules.zhaojiu.service.AttendanceService;
import me.zhengjie.modules.zhaojiu.service.dto.AttendanceDto;
import me.zhengjie.modules.zhaojiu.service.dto.AttendanceQueryCriteria;
import me.zhengjie.modules.zhaojiu.service.mapstruct.AttendanceMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2021-12-09
**/
@Service
@RequiredArgsConstructor
public class AttendanceServiceImpl implements AttendanceService {

    private final AttendanceRepository attendanceRepository;
    private final AttendanceMapper attendanceMapper;

    @Override
    public Map<String,Object> queryAll(AttendanceQueryCriteria criteria, Pageable pageable){
        Page<Attendance> page = attendanceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(attendanceMapper::toDto));
    }

    @Override
    public List<AttendanceDto> queryAll(AttendanceQueryCriteria criteria){
        return attendanceMapper.toDto(attendanceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AttendanceDto findById(Long attendanceId) {
        Attendance attendance = attendanceRepository.findById(attendanceId).orElseGet(Attendance::new);
        ValidationUtil.isNull(attendance.getAttendanceId(),"Attendance","attendanceId",attendanceId);
        return attendanceMapper.toDto(attendance);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AttendanceDto create(Attendance resources) {
        return attendanceMapper.toDto(attendanceRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Attendance resources) {
        Attendance attendance = attendanceRepository.findById(resources.getAttendanceId()).orElseGet(Attendance::new);
        ValidationUtil.isNull( attendance.getAttendanceId(),"Attendance","id",resources.getAttendanceId());
        attendance.copy(resources);
        attendanceRepository.save(attendance);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long attendanceId : ids) {
            attendanceRepository.deleteById(attendanceId);
        }
    }

    @Override
    public void download(List<AttendanceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AttendanceDto attendance : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", attendance.getName());
            map.put("教师ID", attendance.getTeacherId());
            map.put("班级ID", attendance.getClassId());
            map.put("时间", attendance.getDate());
            map.put("状态", attendance.getEnabled());
            map.put("创建者", attendance.getCreateBy());
            map.put("更新者", attendance.getUpdateBy());
            map.put("创建日期", attendance.getCreateTime());
            map.put("更新时间", attendance.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public int count(Long teacherId, Long classId, Long courseId) {
        return attendanceRepository.count(teacherId, classId, courseId);
    }
}
